//
//  JDGoodsListCell.swift
//  DragonWallet
//
//  Created by volantPig on 2024/4/9.
//

import UIKit
import Kingfisher
import SwiftyJSON

class JDGoodsListCell: UITableViewCell {
    
    var goodsModel: JDGoodsModel? {
        didSet {
            self.refreshData()
        }
    }

    fileprivate var containerView = UIView()
    fileprivate var goodsImgV = UIImageView(image: UIImage(named: "loading_placeholder"))
    fileprivate var goodsTitle = UILabel()
    fileprivate var goodsInfo = UILabel()
    fileprivate var goodsPrice = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = .clear //color("#F5F6F8")
        self.backgroundColor = .clear
        self.setupUIElements()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        self.goodsImgV.image = nil
    }
    
    func setupUIElements() {
        
        self.contentView.addSubview(self.containerView)
        self.containerView.snp.makeConstraints { make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-10)
        }
        self.containerView.backgroundColor = .white
        
        self.containerView.addSubview(self.goodsImgV)
        self.goodsImgV.snp.makeConstraints { make in
            make.left.equalTo(self.containerView).offset(12)
            make.centerY.equalTo(self.containerView)
            make.size.equalTo(CGSize(width: 72, height: 72))
        }

        self.containerView.addSubview(self.goodsTitle)
        self.goodsTitle.snp.makeConstraints { make in
            make.left.equalTo(self.goodsImgV.snp.right).offset(15)
            make.top.equalTo(self.goodsImgV)
            make.right.equalTo(self.containerView).offset(-12)
            make.height.equalTo(24)
        }
        self.goodsTitle.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#333333"))
        
        self.containerView.addSubview(self.goodsInfo)
        self.goodsInfo.snp.makeConstraints { make in
            make.left.equalTo(self.goodsImgV.snp.right).offset(15)
            make.top.equalTo(self.goodsTitle.snp.bottom).offset(4)
            make.height.equalTo(19)
        }
        self.goodsInfo.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#333333"))
        self.goodsInfo.text = "产地：日本"
//        fileprivate var goodsPrice = UILabel()
        
        self.containerView.addSubview(self.goodsPrice)
        self.goodsPrice.snp.makeConstraints { make in
            make.bottom.equalTo(self.goodsImgV).offset(-2)
            make.left.equalTo(self.goodsImgV.snp.right).offset(15)
            make.height.equalTo(19)
        }
        self.goodsPrice.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .semibold), textColor: color("#333333"))
        self.goodsPrice.text = "￥700"
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func refreshData() {
        guard let confirmModel = self.goodsModel else { return }
        
        if confirmModel.goodsMainImgUrl?.hasPrefix("http") ?? false  {
            self.goodsImgV.kf.setImage(with: URL(string: (confirmModel.goodsMainImgUrl ?? "/")))
        } else {
            self.goodsImgV.downloadImage(confirmModel.goodsMainImgUrl ?? "")
        }
        self.goodsTitle.text = "「" + (confirmModel.brand ?? "") + "」" + (confirmModel.goodsName ?? "")
//        「上下一白」羊脂玉瓷素烧茶杯酒杯主人杯个
        self.goodsInfo.text = "产地:" + (confirmModel.origin ?? "")
        self.goodsPrice.text = String.init(format: "￥%@", confirmModel.goodsPriceText ?? "0.00")//String.init(format: "￥%.2f", JSON(confirmModel.goodsPrice as Any).floatValue/1000.0)
        self.goodsPrice.priceString(UIFont.systemFont(ofSize: 12, weight: .regular), UIFont.systemFont(ofSize: 16, weight: .semibold))
    }
    
}
